package com.zhouyibike.uu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.MyReceiverResult;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.text.DecimalFormat;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class RaidingActivity extends BaseActivity implements View.OnClickListener{

    private ImageButton mBtnBack;
    private TextView mBtnTitle;

    private TextView tv_hf,tv_hb,tv_mf,tv_mb,tv_sf,tv_sb;
    private Myhandler myhandler;
    private int hf,hb,mf,mb,sf,sb;
    private boolean ismax=false;
    private String plantnum,moneyunit;
    private Long timepast;
    private TextView tv_num,tv_money;

//    private String orderid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_raiding);
        EventBus.getDefault().register(this);
        plantnum=getIntent().getStringExtra("bikenum");
        moneyunit=getIntent().getStringExtra("money_unit");
        timepast=getIntent().getLongExtra("timepast",0L);
//        orderid=getIntent().getStringExtra("bicycleId");
        if(timepast!=0){
            hf=(int)(timepast/3600)/10;
            hb=(int)(timepast/3600)%10;

            mf=(int)(timepast%3600)/60/10;
            mb=(int)(timepast%3600)/60%10;

            sf=(int)(timepast%60)/10;
            sb=(int)(timepast%60)%10;
        }
        myhandler=new Myhandler();
        myhandler.sendEmptyMessageDelayed(1,1000);
        initView();
        setResult(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        toastShow("结束行程");
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void endride(MyReceiverResult progress) {
        if(progress.getPlateNumber().equals(plantnum)){
                Intent intent=new Intent(RaidingActivity.this,RaidingEndActivity.class);
                intent.putExtra("ists",true);
                intent.putExtra("msg",progress);
                intent.putExtra("needpay",progress.isNeedPay());
                startActivityForResult(intent,10000);
        }else{
            return;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==10000&&resultCode==1){//跟钱有关  要重新获取用户余额  看是否余额小于最低数
            finish();
        }
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnBack.setVisibility(View.GONE);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("骑行中");
        mBtnBack.setOnClickListener(this);

        tv_hf= (TextView) findViewById(R.id.tv_rading_hf);
        tv_hb= (TextView) findViewById(R.id.tv_rading_hb);
        tv_mf= (TextView) findViewById(R.id.tv_rading_mf);
        tv_mb= (TextView) findViewById(R.id.tv_rading_mb);
        tv_sf= (TextView) findViewById(R.id.tv_rading_sf);
        tv_sb= (TextView) findViewById(R.id.tv_rading_sb);
        tv_num= (TextView) findViewById(R.id.tv_rading_dcnum);
        tv_money= (TextView) findViewById(R.id.tv_rading_dcmoney);
        tv_num.setText(plantnum);
        tv_money.setText(moneyunit);
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
        }
    }
    DecimalFormat df = new DecimalFormat("######0.00");
    class Myhandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            sb++;
            if(sb==10){
                sb=0;
                sf++;
            }
            if(sf==6){
                sf=0;
                mb++;
            }
            if(mb==10){
                mb=0;
                mf++;
            }
            if(mf==6){
                mf=0;
                hb++;
            }
            if(hb==10){
                hb=0;
                hf++;
            }
            if(hf==10){
                hf=9;
                ismax=true;
            }
            tv_hf.setText(hf+"");
            tv_hb.setText(hb+"");
            tv_mf.setText(mf+"");
            tv_mb.setText(mb+"");
            tv_sf.setText(sf+"");
            tv_sb.setText(sb+"");

            tv_money.setText(df.format((hf*10+hb+1)*Double.parseDouble(moneyunit))+"");
            if(!ismax)
                myhandler.sendEmptyMessageDelayed(1,1000);
        }
    }

}
